//
//  ByHomeSegmentView.m
//  beibei_ios
//
//  Created by pro on 16/6/28.
//  Copyright © 2016年 hebidu. All rights reserved.
//

#import "BySegmentView.h"


@interface BySegmentView ()
{
    NSInteger _prevTag;
}
@property (nonatomic, strong) UIScrollView *scroll; // 底部滑动视图

@property (nonatomic, strong) UIView *selectView;   // 选中视图

@property (nonatomic, strong) NSArray *titlesAry;

@end

@implementation BySegmentView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor whiteColor];
        [self initViews];
    }
    
    
    return self;
}

#pragma mark - setter


#pragma mark - getter
- (NSArray *)titlesAry {
    if (!_titlesAry) {
        _titlesAry = @[@"首页", @"组织机构", @"四方合作", @"人才推荐", @"实践基地", @"合作成果", @"企业信息"];
    }
    return _titlesAry;
}
- (UIView *)selectView {
    if (!_selectView) {
        _selectView = [[UIView alloc] initWithFrame:CGRectMake(0, self.height - 4, 0, 4)];
        _selectView.backgroundColor = JM_THEME_COLOR;
        [self.scroll addSubview:_selectView];
    }
    return _selectView;
}
- (UIScrollView *)scroll {
    if (!_scroll) {
        UIView *view = [[UIView alloc] initWithFrame:self.bounds];
        view.backgroundColor = [UIColor whiteColor];
        [self addSubview:view];
        _scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
        _scroll.showsVerticalScrollIndicator = NO;
        _scroll.showsHorizontalScrollIndicator = NO;
        [self addSubview:_scroll];
    }
    return _scroll;
}
- (void)initViews {
    CGFloat fontSize = 14; // 字体大小
    CGFloat x = 5, y, width, height;
    // 循环创建分栏按钮
    NSMutableArray *widths = [[NSMutableArray alloc] init];         // 记录宽度
    for (NSString *title in self.titlesAry) {
        CGFloat titleWidth = [title getSizeOfString:fontSize].width + 20;
        [widths addObject:@(titleWidth)];
    }
    
    for (UIView *subview in self.scroll.subviews) {
        if ([subview isKindOfClass:[UIButton class]]) {
            [subview removeFromSuperview];
        }
    }
    for (int i = 0; i < self.titlesAry.count; i ++) {
        if (i == 0) {
        } else {
            x += [widths[i - 1] floatValue];
        }
        y = 0;
        width = [widths[i] floatValue];
        height = self.height;
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(x, y, width, height);
        button.backgroundColor = [UIColor clearColor];
        [button setTitle:self.titlesAry[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitleColor:JM_THEME_COLOR forState:UIControlStateSelected];
        button.titleLabel.font = FONT(fontSize);
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.scroll addSubview:button];
        
        button.tag = 10 + i;
        if (i == 0) {
            button.selected = YES;
            self.selectView.width = button.width - 20;
            self.selectView.center = CGPointMake(button.center.x, self.selectView.center.y);
            _prevTag = button.tag;
            
        }
        
        if (i == self.titlesAry.count - 1) {
            self.scroll.contentSize = CGSizeMake(button.right + 10, 0);
        }
    }
    
    
    
    // 创建底部线
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 1)];
    line.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    line.bottom = self.height;
    [self addSubview:line];

}
#pragma mark - 绘制
//- (void)drawRect:(CGRect)rect {
//
//    [super drawRect:rect];
//    NSLog(@"重新绘制了");
//
//    CGFloat fontSize = 14; // 字体大小
//    CGFloat x = 5, y, width, height;
//    // 循环创建分栏按钮
//    NSMutableArray *widths = [[NSMutableArray alloc] init];         // 记录宽度
//    for (NSString *title in self.titlesAry) {
//        CGFloat titleWidth = [title getSizeOfString:fontSize].width + 20;
//        [widths addObject:@(titleWidth)];
//    }
//    
//    for (UIView *subview in self.scroll.subviews) {
//        if ([subview isKindOfClass:[UIButton class]]) {
//            [subview removeFromSuperview];
//        }
//    }
//    for (int i = 0; i < self.titlesAry.count; i ++) {
//        if (i == 0) {
//        } else {
//            x += [widths[i - 1] floatValue];
//        }
//        y = 0;
//        width = [widths[i] floatValue];
//        height = self.height;
//        
//        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//        button.frame = CGRectMake(x, y, width, height);
//        button.backgroundColor = [UIColor clearColor];
//        [button setTitle:self.titlesAry[i] forState:UIControlStateNormal];
//        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        [button setTitleColor:JM_THEME_COLOR forState:UIControlStateSelected];
//        button.titleLabel.font = FONT(fontSize);
//        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//        [self.scroll addSubview:button];
//        
//        button.tag = 10 + i;
//        if (i == 0) {
//            button.selected = YES;
//            self.selectView.width = button.width - 20;
//            self.selectView.center = CGPointMake(button.center.x, self.selectView.center.y);
//            _prevTag = button.tag;
//            
//        }
//        
//        if (i == self.titlesAry.count - 1) {
//            self.scroll.contentSize = CGSizeMake(button.right + 10, 0);
//        }
//    }
//
//   
//    
//    // 创建底部线
//    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 1)];
//    line.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
//    line.bottom = self.height;
//    [self addSubview:line];
//
//}

- (void)setIndex:(NSInteger)index {
    UIButton *button = (UIButton *)[self viewWithTag:index + 10];
    [self buttonAction:button];
}

#pragma mark - 点击事件
- (void)buttonAction:(UIButton *)button {
    
    // 点击了已经选中的按钮
    if (button.tag == _prevTag) {
        return;
    }
    // 更改上个呗选中的按钮的状态
    UIButton *prevButton = (UIButton *)[self viewWithTag:_prevTag];
    prevButton.selected = NO;
    
    // 滚动标题栏到中间位置
    CGFloat offsetx   = button.center.x - self.scroll.width * 0.5;
    CGFloat offsetMax = self.scroll.contentSize.width - self.scroll.width;
    
    // 在最左和最右时，标签没必要滚动到中间位置。
    if (offsetx < 0) {
        offsetx = 0;
    }
    if (offsetx > offsetMax) {
        offsetx = offsetMax;
    }
    [self.scroll setContentOffset:CGPointMake(offsetx, 0) animated:YES];
    
    // 更改上个被选中的索引
    button.selected = YES;
    _prevTag = button.tag;
    [UIView animateWithDuration:0.2 animations:^{
        self.selectView.width = button.width - 20;
        self.selectView.center = CGPointMake(button.center.x, self.selectView.center.y);
    }];

    
    if (self.indexChangeBlock) {
        self.indexChangeBlock(button.tag - 10);
    }
}


@end
